//
//  staffRoomTimeCell.m
//  QRMaster
//
//  Created by myApple on 15/7/25.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "staffRoomTimeCell.h"
#import "setTimeModel.h"

@interface staffRoomTimeCell()
@property(nonatomic,strong)UILabel * nameLabel;
@property(nonatomic,strong)AllocateRoomModel * roomInfo;
@property(nonatomic,copy)NSString * roomid;
@property(nonatomic,copy)NSString * roomName;
@property(nonatomic,copy)NSString * roomno;
@property(nonatomic,strong)UIView * lineView;
@property(nonatomic,strong)UIImageView * roomImgView;


//设置职员房间时间属性的
@property(nonatomic,retain)NSMutableArray * timeArray;

@property(nonatomic,retain)NSMutableArray * addColorTimeArray;

@end

@implementation staffRoomTimeCell

+(instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString * identityId = @"roomTableViewCell";
    staffRoomTimeCell * cell = [tableView dequeueReusableCellWithIdentifier:identityId];
    if (!cell) {
        cell = [[staffRoomTimeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identityId];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self addAllSubView];
        [self prepareView];
    }
    return self;
}

-(void)addAllSubView
{
    [self addSubview:self.nameLabel];
    [self addSubview:self.lineView];
    [self addSubview:self.roomImgView];
}

#pragma mark ----------initData
-(void)setData:(AllocateRoomModel *)roomInfo
{
    self.roomInfo = roomInfo;
    self.nameLabel.text = self.roomInfo.roomName;
    self.addColorTimeArray = self.roomInfo.timeArray;
    self.timeArray = self.allTimeArray;
    self.roomid = self.roomInfo.roomid;
    self.roomno = self.roomInfo.roomno;
    _roomImgView.backgroundColor = [UIColor redColor];
    
    
}

-(void)setAllTimeArray:(NSMutableArray *)allTimeArray
{
    _allTimeArray = allTimeArray;
}

-(void)setTimeArray:(NSMutableArray *)timeArray
{
    for (int i = 0; i < 3; i ++) {
        UIButton * btn = (UIButton *)[self viewWithTag:5320 +i];
        btn.hidden = YES;//防止复用图片
        btn.backgroundColor = [UIColor whiteColor];
    }
    _timeArray = timeArray;
}


#pragma mark ---------prepareView
-(void)prepareView
{
    float width = 16;
    float x = 0;
    float y = (self.frame.size.height - 16)/2.0;
    float height = 16;
    for (int i = 0; i < 3; i++) {
        x += width + 10;
        width = 16;
        UIButton * timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        timeBtn.hidden = YES;
        timeBtn.tag = 5320 + i;
        timeBtn.frame = CGRectMake(x, y, width, height);
        [self.contentView addSubview:timeBtn];
        [timeBtn setImage:[UIImage imageNamed:[NSString stringWithFormat: @"roomTime%d",(i + 1)]] forState:0];
    }

}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat xOffSet = 15;
    if (self.roomInfo) {
        xOffSet = 22;
    }
    
    _roomImgView.frame = CGRectMake(xOffSet, (self.frame.size.height - 28)/2.0, 28, 28);
    
    _nameLabel.frame = CGRectMake(xOffSet + 28 + 16, (self.frame.size.height - 20)/2.0, self.frame.size.width - 16 - 90, 20);
    if (self.isLastRow) {
        self.lineView.frame = CGRectMake(0, self.frame.size.height - 0.5, self.frame.size.width, 0.5);
    }
    else
    {
        self.lineView.frame = CGRectMake(xOffSet, self.frame.size.height - 0.5, self.frame.size.width - xOffSet, 0.5);
    }
    
    float width = [self calculateWidth:self.roomInfo.roomName];
    width += self.nameLabel.frame.origin.x;
    
    NSInteger index = 0;
    for (setTimeModel * timeModel in _timeArray) {
        UIButton * btn = (UIButton *)[self viewWithTag:5320 + index];
        for (int i = 0 ; i < _addColorTimeArray.count; i ++)
        {
            setTimeModel * model = _addColorTimeArray[i];
            if ([model.timeId isEqualToString:timeModel.timeId])
            {
                btn.hidden = NO;
                btn.frame = CGRectMake(width + 16 + 26 * i, btn.frame.origin.y, 16, btn.frame.size.height);
            }
        }
        index ++;
    }

    

}

#pragma mark -----lazy

-(UIImageView *)roomImgView
{
    if (!_roomImgView) {
        _roomImgView = [[UIImageView alloc] initWithFrame:CGRectMake(22,  (self.frame.size.height - 28)/2.0, 28, 28)];
        _roomImgView.image = [UIImage imageNamed:@"assignTimeImage"];
    }
    return _roomImgView;

}

-(UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = [UIFont systemFontOfSize:16.0];
    }
    return _nameLabel;
}

-(UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = UIColorFromRGB(0xc1c1c1);
    }
    return _lineView;
}


-(void)setIsLastRow:(BOOL)isLastRow
{
    _isLastRow = isLastRow;
}

#pragma mark -- roomNo自适应长度
-(CGFloat)calculateWidth:(NSString *)str
{
    CGSize size =CGSizeMake(300,60);
    
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:20.0],NSFontAttributeName,nil];
    
    //ios7方法，获取文本需要的size，限制宽度
    
    CGSize  actualsize =[str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
    return actualsize.width;
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
